<?

function vc_install() {
	global $wpdb;
	timer_start();	
	/*===	Installation of databases	===*/
	
	$table_prefix	=	$wpdb->prefix;
	$table_name		=	$table_prefix . 'vc_forrest';
	trigger_error('VC: Installing table vc_forrest;');
	
	if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name) {
		//	Query for creation of statistics table
		$query	=	<<<SQL
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`id` INT( 11 ) NOT NULL COMMENT 'Identifier of the entry',
`views` INT( 11 ) NOT NULL COMMENT 'Count of views',
`visitors` TEXT CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'Count of visitors',
INDEX ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin COMMENT = 'Views Counter data source table'
SQL;
		//	Creating table with function query() of WPDB class
		$wpdb->query($query);
	}

	$table_name	=	$table_prefix . 'vc_options';
	trigger_error('VC: Installing table vc_options;');
	
	if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name) {
		$query	=	<<<SQL
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'The name of VC option',
`value` TEXT CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'The value of VC option',
PRIMARY KEY ( `name` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin COMMENT = 'The table contains options of VC plugin for Wordpress'
SQL;
		$wpdb->query($query);
	}
		
	//$wpdb->store_options(array('installed.db' => TRUE));
	
	//	If options are not migrated, do it
	/*	Some stuff to migrate options	*/
	
	$table_name	=	$table_prefix . 'vc_data';
	trigger_error('VC: Attempting to migrate database;');
	
	if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") == $table_name) {
		$vcdb	=	new	VC_DB_WP(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
		$vcdb->set_prefix($wpdb->prefix);
		
		set_time_limit(300);
		//register_shutdown_function('vc_migration_not_finished');
		$vcdb->migrate_database();
	}
}

function vc_migration_not_finished() {
	$time	=	timer_stop();
	trigger_error('VC: Migration of database not finished, but the script has terminated. Execution time: ' . $time . ' sec. Will continue next time;', E_USER_WARNING);
}

?>